<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>state练习-简易轮播图</title>
    <style>
        *{
            margin:0;padding:0;
            user-select: none;
        }
        .container{
            width: 640px;
            height: 360px;
            margin: 100px auto;
            position:relative;
        }
        img{
            width: 100%;
        }
        .arrow{
            width: 50px;
            height: 50px;
            background: white;
            border-radius: 50%;
            position:absolute;
            top: 150px;
            text-align:center;
            line-height: 50px;
            font-size: 24px;
        }
        .left{
            left: 20px;
        }
        .right{
            right: 20px;
        }
        body{
            height: 100vh;
            background: radial-gradient(#ccc, #238);
        }
    </style>
</head>
<body>
    <div id="root">
        

    </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.js"></script>

    <script type="text/babel">
        class App extends React.Component {

            state = {
                index: 0,
                images: [
                    './images/1.jpg',
                    './images/2.jpg',
                    './images/3.jpg',
                    './images/4.jpg',
                    './images/5.jpg',
                ]
            }

            render(){
                //获取 index 和 images 数据
                let {index, images} = this.state;
                
                return <div className="container">
                    <div className="img">
                        <img src={images[index]} alt="" />
                    </div>
                    <div className="arrow left" onClick={this.prev}> &lt; </div>
                    <div className="arrow right" onClick={this.next}> &gt; </div>
                </div>
            }   

            //切换下一章图片
            next = () => {
                //获取当前 index 的值
                let {index, images} = this.state;
                //判断 如果到了边界 设置为 0 
                if(index >= images.length - 1){
                    index = 0;
                }else{
                    //如果没有到边界, index 自增
                    index += 1;
                }
                //设置新的 index
                this.setState({
                    index
                })
            }
        
            //切换上一张图片
            prev = () => {
                //获取 index 的值
                let {index, images} = this.state;
                //判断
                if(index <= 0){
                    index = images.length - 1;
                }else{
                    //index自减
                    index = index - 1;
                }
                //设置
                this.setState({
                    index
                })
            }
        }

        //渲染
        ReactDOM.render(<App />, document.querySelector('#root'));
    </script>
</body>
</html>